package com.trkj.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.dao.jpa.IDepositTableDao;
import com.trkj.dao.jpa.IDepositreturnTableDao;
import com.trkj.dao.jpa.IDepositurgeTableDao;
import com.trkj.dao.mybatis.DepositTableMapper;
import com.trkj.pojos.DepositTable;
import com.trkj.pojos.DepositreturnTable;
import com.trkj.pojos.DepositurgeTable;

@Service
@Transactional
public class DepositService {
	@Autowired 
	private IDepositTableDao iDepositTableDao;
	@Autowired
	private DepositTableMapper depositTableMapper;
	@Autowired
	private IDepositreturnTableDao iDepositreturnTableDao;
	@Autowired
	private IDepositurgeTableDao iDepositurgeTableDao;
	
	/**
	 * 拼接退还押金号
	 * @return
	 */
	public String createRetuenDepositID(){
		String RetuenDepositID = "";
		//将当前日期格式化为纯数字
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String str = sdf.format(new Date()).substring(2, 8);
		String LikeID = "%" + str + "%";
		List<String> list = iDepositreturnTableDao.queryReturnDepositID(LikeID);

		if(list.isEmpty()){
			RetuenDepositID = "RT" + str +"0001";
		}else{
			List<Integer> lists = new ArrayList<>();
			for(int i=0;i<list.size();i++){
				lists.add(Integer.valueOf(list.get(i).toString().substring(8, 12)));
			}
			//Collections.max取最大值再加1，拼接住院号
			 int next = Collections.max(lists).intValue()+1;
			 String nextID = String.format("%04d", next);
			 RetuenDepositID = "RT" + str + nextID;
		}
		return RetuenDepositID;
	}
	/**
	 * 根据住院号查询住院押金
	 * @param liveID
	 * @return
	 */
	public List<DepositTable> queryDepositByLiveID(String liveID){
		List<DepositTable> listall  = iDepositTableDao.findByLiveid5(liveID);
		return listall;
		
	}
	/**
	 * 动态分页查询住院押金
	 * @param page
	 * @param rows
	 * @param map
	 * @return
	 */
	public Map<String,Object> fuzzyQueryDeposit(int page, int rows,Map<String,Object> map){
		
		Page<DepositTable> p = PageHelper.startPage(page, rows, true);
		List<DepositTable> list = depositTableMapper.fuzzyfindDeposit(map);
		Map<String,Object> map2 = new HashMap<>();
		map2.put("rows", list);
		map2.put("total", p.getTotal());
		return map2;
		
	}
	/**
	 * 收取押金  修改押金状态
	 * @param depositID
	 * @param takeMoney
	 * @param admin
	 */
	public void  chargeDeposit(String depositID,Double takeMoney,String admin){
		
		BigDecimal bd = new BigDecimal(takeMoney);
		
		DepositTable depositTable =  iDepositTableDao.findOne(depositID);
		depositTable.setDepositstatus("已缴");
		depositTable.setTakelimit(bd);
		depositTable.setTakeadmin(admin);
		iDepositTableDao.save(depositTable);
	}
	/**
	 * 新增返回押金记录  并且修改押金状态
	 * @param depositreturnTable
	 */
	public void addNewDepositReturn(DepositreturnTable depositreturnTable){
		if(depositreturnTable!=null){
			iDepositreturnTableDao.save(depositreturnTable);
			
			DepositTable depositTable = iDepositTableDao.findOne(depositreturnTable.getDepositid());
			depositTable.setDepositstatus("已退");
			iDepositTableDao.save(depositTable);
		}
	}
	/**
	 * 动态查询退还押金
	 * @param page
	 * @param rows
	 * @param map
	 * @return
	 */
	public Map<String,Object> fuzzyqueryReturnDeposit(int page,int rows,Map<String,Object> map){

		Page<DepositreturnTable> p = PageHelper.startPage(page, rows, true);
		List<DepositreturnTable> list = depositTableMapper.fuzzyfindReturnDeposit(map);
		
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("total", p.getTotal());
		resultMap.put("rows", list);
		return resultMap;
	}
	
	/**
	 * 拼接催费押金号
	 * @return
	 */
	public String createUrgeDepositID(){
		String UrgeDepositID = "";
		//将当前日期格式化为纯数字
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String str = sdf.format(new Date()).substring(2, 8);
		String LikeID = "%" + str + "%";
		List<String> list = iDepositurgeTableDao.queryUrgeDepositID(LikeID);

		if(list.isEmpty()){
			UrgeDepositID = "UD" + str +"0001";
		}else{
			List<Integer> lists = new ArrayList<>();
			for(int i=0;i<list.size();i++){
				lists.add(Integer.valueOf(list.get(i).toString().substring(8, 12)));
			}
			//Collections.max取最大值再加1，拼接住院号
			 int next = Collections.max(lists).intValue()+1;
			 String nextID = String.format("%04d", next);
			 UrgeDepositID = "UD" + str + nextID;
		}
		return UrgeDepositID;
	}
	/**
	 * 添加催缴押金记录
	 * @param depositurgeTable
	 */
	public void addUrgeDeposit(DepositurgeTable depositurgeTable){
		if(depositurgeTable!=null){
			iDepositurgeTableDao.save(depositurgeTable);
		}
	}
	
	/**
	 * 动态查询催缴单
	 * @param page
	 * @param rows
	 * @param map
	 * @return
	 */
	public Map<String,Object> fuzzyqueryUrgeDeposit(int page,int rows,Map<String,Object> map){

		Page<DepositurgeTable> p = PageHelper.startPage(page, rows, true);
		List<DepositurgeTable> list = depositTableMapper.fuzzyfindUrgeDeposit(map);
		
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("total", p.getTotal());
		resultMap.put("rows", list);
		return resultMap;
	}
	/**
	 * 拼接押金号
	 * @return
	 */
	public String createDepositID(){
		String DepositID = "";
		//将当前日期格式化为纯数字
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String str = sdf.format(new Date()).substring(2, 8);
		String LikeID = "%" + str + "%";
		List<String> list = iDepositTableDao.queryDepositID(LikeID);

		if(list.isEmpty()){
			DepositID = "DP" + str +"0001";
		}else{
			List<Integer> lists = new ArrayList<>();
			for(int i=0;i<list.size();i++){
				lists.add(Integer.valueOf(list.get(i).toString().substring(8, 12)));
			}
			//Collections.max取最大值再加1，拼接住院号
			 int next = Collections.max(lists).intValue()+1;
			 String nextID = String.format("%04d", next);
			 DepositID = "DP" + str + nextID;
		}
		return DepositID;
	}

}
